package com.kedou.sdk.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;

import androidx.fragment.app.FragmentActivity;

import com.kedou.sdk.params.bean.SimpleUserInfo;
import com.kedou.sdk.ui.assist.AccountChangeListAdapter;
import com.kedou.sdk.ui.assist.AccountListAdapter;
import com.kedou.sdk.ui.assist.OnFinishActivityListener;

/**
 * Created by Administrator on 2018/3/8.
 */
public class AccountListChangeDialogFragment extends BaseDialogFragment implements View.OnClickListener, AdapterView.OnItemClickListener {


    private Button mConfirmLoginBtn;
    private ImageButton mReturnLoginBtn;
    private ListView mAccountListView;

//    private View lastSelectedView;
    private int lastSelectedPosition;

    private AccountChangeListAdapter mListAdapter;

    public static final AccountListChangeDialogFragment newInstance(Bundle bundle) {
        AccountListChangeDialogFragment fragment = new AccountListChangeDialogFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View contentView = doInflateContentView(inflater, container, getLayoutIdentifier("hnssn_activity_accountchange_lists"));

        mConfirmLoginBtn = contentView.findViewById(getViewIdentifier("activity_account_login_btn"));
        mAccountListView = contentView.findViewById(getViewIdentifier("activity_account_user_account_lv"));
        mReturnLoginBtn = contentView.findViewById(getViewIdentifier("activity_forgetpassword_finish_btn"));
//        TextView cellphoneText = contentView.findViewById(getViewIdentifier("activity_account_user_cellphone"));

        mConfirmLoginBtn.setOnClickListener(this);
        mReturnLoginBtn.setOnClickListener(this);
        mAccountListView.setOnItemClickListener(this);

        mListAdapter = new AccountChangeListAdapter();

        Bundle bundle = getArguments();
        if (bundle != null) {
//            cellphoneText.setText(bundle.getString("phone"));
            mListAdapter.setAdapterData(bundle.<SimpleUserInfo>getParcelableArrayList("userInfos"));
        }

        mAccountListView.setAdapter(mListAdapter);
        return contentView;
    }

    @Override
    public void onClick(View view) {
        if (mConfirmLoginBtn == view) {
            OnUserLoginActionListener listener = getOnUserLoginActionListener();
            if (listener != null) {
                SimpleUserInfo userInfo = mListAdapter.getItem(lastSelectedPosition);
                listener.doChangeSelectAccount(userInfo);
            }
        } else {
            OnUserLoginActionListener listener = getOnUserLoginActionListener();
            if (listener != null) {
                listener.doFinishActivity(this);
            }
        }
    }

    private OnUserLoginActionListener getOnUserLoginActionListener() {
        if (!isNullContext()) {
            FragmentActivity activity = getActivity();
            if (activity instanceof OnUserLoginActionListener) {
                return (OnUserLoginActionListener) activity;
            }
        }
        return null;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        this.lastSelectedPosition = position;
        mListAdapter.setLastSelectedIndex(position);
//        if (lastSelectedView == null) {
//            View firstView = parent.getChildAt(parent.getFirstVisiblePosition());
//            resetItemViewSelectedState(firstView, false);
//        }
//
//        if (lastSelectedView != view) {
//            resetItemViewSelectedState(lastSelectedView, false);
//            resetItemViewSelectedState(view, true);
//            this.lastSelectedView = view;
//            this.lastSelectedPosition = position;
//            mListAdapter.setLastSelectedIndex(position);
//        }
    }

    private void resetItemViewSelectedState(View view, boolean selected) {
        if (view != null) {
            Object obj = view.getTag();
            if (obj instanceof AccountListAdapter.ViewHolder) {
                AccountListAdapter.ViewHolder viewHolder = (AccountListAdapter.ViewHolder) obj;
                viewHolder.checked.setChecked(selected);
                viewHolder.account.setSelected(selected);
            }
        }
    }

    public interface OnUserLoginActionListener extends OnFinishActivityListener {

        void doChangeSelectAccount(SimpleUserInfo userInfo);
    }
}
